﻿using Microsoft.AspNet.SignalR;
using System;
using System.Threading.Tasks;
using RssEasy.Core;
using LocalDAL;
using Microsoft.AspNet.SignalR.Hubs;

[HubName("ChatHub")]
public class ChatHub : Hub
{
    /// <summary>
    /// 用户登陆
    /// </summary>
    /// <param name="myid"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public string UserLogin(string myid)
    {
        try
        {
            UserList entity = new UserList();
            entity.remove("myid");
            entity.keyvalue("connectionid", Context.ConnectionId).timestamp("unlinetime");
            entity.columnvalue("offlinetime", "NULL");
            entity.update().where("myid=@myid", myid).submit();

            return Context.ConnectionId;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    public void UserLogin(string myid, string tomyid)
    {
        this.UserLogin(myid);
        this.GetHistoryList(myid, tomyid);
    }
    public void GetHistoryList(string myid, string tomyid)
    {
        ChatList list = new ChatList();
        list.select("msg,shijian").where("myid=@myid and tomyid=@tomyid", tomyid, myid).query();

        Clients.Caller.historyList(JsonExtend.toKeyValue(list.datatable));

        list.delete().where("myid=@myid and tomyid=@tomyid", tomyid, myid).submit();
    }
    public void BroadCast(string title, string message)
    {
        Clients.All.broadCast(title, message);
    }
    public void BroadCast(string myid, string title, string message, string marker)
    {
        UserList entity = new UserList();
        if (entity.select("connectionid").where("myid=@myid and connectionid is not null", myid).get_first_rows())
        {
            Clients.Client(entity["connectionid"]).broadCast(title, message, marker, DateTimeExtend.timestamp());
        }
    }
    public void UserBroadCast(string myid, string title, string message, string marker)
    {
        UserList entity = new UserList();
        if (entity.select("connectionid,nickname,realname").where("myid=@myid and connectionid is not null", myid).get_first_rows())
        {
            Clients.Others.userBroadCast(entity["nickname"], entity["realname"], title, message, marker, DateTimeExtend.timestamp());
        }
    }
    public void NotityUseMyID(string myid, string title, string message)
    {
        UserList entity = new UserList();
        if (entity.select("connectionid").where("myid=@myid and connectionid is not null", myid).get_first_rows())
        {
            Notity(myid, title, message);
        }
    }
    public void Notity(string connectionid, string title, string message)
    {
        Clients.Client(connectionid).notity(title, message, DateTimeExtend.timestamp());
    }
    public override Task OnDisconnected(bool stopCalled)
    {
        UserList entity = new UserList();
        entity.timestamp("offlinetime");
        entity.update().where("connectionid=@connectionid", Context.ConnectionId).get_first_rows();
        return base.OnDisconnected(stopCalled);
    }

    /// <summary>
    /// 给他发送聊天信息
    /// </summary>
    /// <param name="myid"></param>
    /// <param name="realname"></param>
    /// <param name="tomyid"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public string ChatToMyID(string myid, string tomyid, string msg)
    {
        try
        {
            UserList entity = new UserList();
            if (entity.select("connectionid,offlinetime").where("myid=@myid and connectionid is not null", tomyid).get_first_rows())
            {
                if (string.IsNullOrEmpty(entity["offlinetime"]) == false)
                {
                    ChatList chat = new ChatList();
                    chat.keyvalue("myid", myid).keyvalue("tomyid", tomyid).keyvalue("msg", msg).timestamp();
                    chat.append().submit();
                    return chat.autoid.ToString();
                }
                else
                {
                    Clients.Client(entity["connectionid"]).getToMyID(myid, msg, DateTimeExtend.timestamp());
                }
            }
            return null;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    /// <summary>
    /// 给他发送聊天信息并保存
    /// </summary>
    /// <param name="myid"></param>
    /// <param name="realname"></param>
    /// <param name="tomyid"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public string ChatToMyIDUseSave(string myid, string tomyid, string msg)
    {
        try
        {
            ChatToMyID(myid, tomyid, msg);

            ChatList entity = new ChatList();
            entity.keyvalue("myid", myid).keyvalue("tomyid", tomyid).keyvalue("msg", msg).timestamp();
            entity.append().submit();

            return entity.autoid.ToString();

        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}